var utils = require("../../utils/utils");
var time = require("../../utils/time");
var ocrGml = require("../../utils/ocrGml");
var readArticle = require("../appCommon/readArticle");
var swipeUp = require("../appCommon/swipeUp");
var swipeDown = require("../appCommon/swipeDown");
var pointMap = {
  首页任务: {
    points: [
      // prettier-ignore
      418, 2180, 660, 2296,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  首页我的: {
    points: [
      // prettier-ignore
      858, 2180, 1056, 2296,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
  微信提现关闭: {
    points: [
      // prettier-ignore
      847, 599, 885, 638,
    ],
    size: {
      x: 1080,
      y: 2340,
    },
  },
};
function inHome() {
  // prettier-ignore
  var contents = [
          "视频",
          "小说"
      ]
  var result = ocrGml.captureExistAll(contents);
  if (result) {
    console.log("找到-" + contents.join("和") + "-,判断在首页");
  } else {
    console.log("没找到-" + contents.join("和") + "-");
  }
  return result;
}
function toTask() {
  console.log("前往-首页任务-");
  utils.clickPointMap(pointMap["首页任务"]);
}

function ifHasNextAdDefault() {
  var contents = [
    //prettier-ignore
    "看视频领",
    "看视频再领",
    "看视频再得",
    "看广告领",
    "看广告再领",
    "看广告再得",
    "再看一个",
    "再看1个",
  ];
  for (var index = 0; index < contents.length; index++) {
    var content = contents[index];
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      console.log("找到-" + content + "-", ",存在下一个广告可看");
      return {
        result: true,
        trigger: function () {
          utils.clickUIText(UIText);
        },
      };
    }
  }
  return {
    result: false,
    trigger: function () {},
  };
}
function ifAdOverDefault() {
  var UIText = utils.existsTextStartsWith("领取成功");
  if (UIText) {
    console.log("找到-领取成功-");
    return UIText;
  }
  var UIText = utils.existsTextStartsWith("已领取");
  if (UIText) {
    console.log("找到-已领取-");
    return UIText;
  }
  var UIText = utils.existsTextStartsWith("已完成任务");
  if (UIText) {
    console.log("找到-已完成任务 -");
    return UIText;
  }
}
function existAdDefault() {
  var contents = [
    //prettier-ignore
    "残忍离开",
    "坚持离开",
    "残忍退出",
    "坚持退出",
  ];
  for (var index = 0; index < contents.length; index++) {
    var content = contents[index];
    var UIText = utils.existsTextStartsWith(content);
    if (UIText) {
      console.log("找到-" + content + "-，", "关闭广告");
      utils.clickUIText(UIText);
    }
  }
}

var AD_CONST_MIN_INTERVAL = 40;
var AD_CONST_MAX_GO_BACK_TIMES = 4;

var ad = {
  inStart: false,
  inHome: false,
  ifHasNextAd: null, //判断是否有下一个广告
  ifAdOver: null, //判断广告实际是否比预计时间提前结束
  ifInStart: null, //判断是否是在任务页
  ifInHome: null, //判断是否在首页
  hasWatchTimes: 0,
  maxTimes: 0,
  init(maxTimes, ifInStart, ifHasNextAd, ifAdOver, exitAd, ifInHome) {
    this.hasWatchTimes = 0;
    this.maxTimes = utils.getRandom(maxTimes, maxTimes + 2);
    this.ifInStart = ifInStart;
    this.ifHasNextAd = ifHasNextAd || ifHasNextAdDefault;
    this.ifAdOver = ifAdOver || ifAdOverDefault;
    this.exitAd = exitAd || existAdDefault;
    this.ifInHome = ifInHome || inHome;
    console.show();
    console.log("ad init: 本轮最多看-" + this.maxTimes + "-个广告");
    this.run();
  },
  run() {
    //开始看广告
    var adSpan = AD_CONST_MIN_INTERVAL + utils.getRandom(0, 5);
    console.log(
      //prettier-ignore
      "ad run: 开始看-" + (this.hasWatchTimes+1)+"/" + this.maxTimes + "-个广告，预计时间-" + adSpan + "-s"
    );
    var checkAdOverSpan = 2; //每隔2s检查当前广告是否已经结束
    var isOverEarly = false;
    var hasWatchSpan = 0;
    var count = 0;
    while (adSpan > checkAdOverSpan) {
      if (hasWatchSpan > 30) {
        if (!utils.existsTextStartsWith("广告")) {
          console.log(
            //prettier-ignore
            "ad run: 已过-" + hasWatchSpan + "-s但是没找到-广告-,即将执行一次back()"
          );
          back();
          sleep(utils.getRandom(2, 2.5, 1000));
        }
      }
      if (this.ifAdOver && this.ifAdOver()) {
        var waitSpan = utils.getRandom(2, 3.5);
        console.log("ad run: 广告已结束,再过" + waitSpan + "s后停止当前广告");
        sleep(waitSpan * 1000);
        isOverEarly = true; //广告实际比预计的时间提前结束
        break;
      }
      sleep(checkAdOverSpan * 1000);
      adSpan = adSpan - checkAdOverSpan;
      hasWatchSpan = hasWatchSpan + checkAdOverSpan;
      count = count + 1;
      if (count % 5 == 0) {
        console.log("ad run: 广告时间预计还剩-" + adSpan + "-s");
      }
    }
    this.hasWatchTimes = this.hasWatchTimes + 1;
    if (isOverEarly) {
      this.goBack();
      return;
    }
    if (adSpan > 0) {
      console.log("ad run: 广告时间还剩最后的" + adSpan + "秒");
      sleep(adSpan * 1000);
      this.goBack();
      return;
    }
  },
  goBack() {
    var index = 0;
    while (index < AD_CONST_MAX_GO_BACK_TIMES) {
      console.log("ad goBack: 第" + (index + 1) + "次执行back");
      back();
      sleep(utils.getRandom(2.5, 4, 1000));
      var hasNextAd = this.ifHasNextAd();
      if (hasNextAd.result) {
        if (this.hasWatchTimes >= this.maxTimes) {
          console.log(
            //prettier-ignore
            "当前看广告-" + this.hasWatchTimes + "-次，最多看-" + this.maxTimes + "-次，执行exitAd"
          );
          this.exitAd();
          this.after();
          return;
        } else {
          hasNextAd.trigger();
          sleep(utils.getRandom(2, 3, 1000));
          this.run();
          return;
        }
      }
      if (this.ifInStart && this.ifInStart()) {
        console.log("ad goBack: 判断在开始页");
        this.inStart = true;
        this.after();
        return;
      }
      if (this.ifInHome && this.ifInHome()) {
        console.log("ad goBack: 判断在app首页");
        this.inHome = true;
        this.after();
        return;
      }
      index = index + 1;
    }
    this.after();
  },
  after() {
    console.log("ad after");
    console.hide();
    this.next();
  },
  next() {
    console.log("ad next");
  },
};

var storage = utils.getStorage(time.str("YYYY-MM-DD") + "百度极速");
exports.toTask = toTask;
exports.inHome = inHome;
exports.pointMap = pointMap;
exports.swipeUp = swipeUp;
exports.swipeDown = swipeDown;
exports.readArticle = readArticle;
exports.ad = ad;
exports.storage = storage;
